﻿/*
 * Copyright 2010 Pieter De Rycke
 *
 * This file is part of Frozen Bubble XNA.
 *
 * Frozen Bubble XNA is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * Frozen Bubble XNA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Frozen Bubble XNA. If not, see 
 * <http://www.gnu.org/licenses/>.
 *
 * Artwork:
 *    Alexis Younes <73lab at free.fr>
 *      (everything but the bubbles)
 *    Amaury Amblard-Ladurantie <amaury at linuxfr.org>
 *      (the bubbles)
 *
 * Soundtrack:
 *    Matthias Le Bidan <matthias.le_bidan at caramail.com>
 *      (the three musics and all the sound effects)
 *
 * Design & Programming:
 *    Guillaume Cottenceau <guillaume.cottenceau at free.fr>
 *      (design and manage the project, whole Perl sourcecode)
 *
 * XNA and Windows Phone 7 port:
 *    Pieter De Rycke
 *
 * Websites:
 *    http://www.frozen-bubble.org/
 *    http://pieterderycke.wordpress.com/
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.GamerServices;

namespace FrozenBubble.Screens
{
    public class LosePanelScreen : PanelScreen
    {
        private int _level;
        private int _playTime;

        private InputState _oldInput;

        public LosePanelScreen(Vector2 position, int level, int playTime)
            : base("Graphics/Panels/lose_panel", position)
        {
            _level = level;
            _playTime = playTime;
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if (!otherScreenHasFocus)
            {
                InputState input = Input.State;

                if (!input.IsActionActive(InputAction.Any) && 
                    _oldInput.IsActionActive(InputAction.Any))
                {
                    if (ScreenManager.HighScoreManager.IsHighScore(_level, _playTime))
                    {
                        InputBoxScreen.ShowInputBoxScreen("Congratulations!",
                                                          "You have a highscore!\nPlease enter your name:",
                                                          "",
                                                          (userName) =>
                                                          {
                                                              SaveHighScore(userName);
                                                              ExitScreen();
                                                          });
                    }
                }

                _oldInput = input;
            }
        }

        private void SaveHighScore(string userName)
        {
            if (userName != null)
            {
                Storage storage = ScreenManager.Storage;
                storage.HighScores.AddHighScore(userName, _level + 1, _playTime, false);
                storage.Save();
            }
        }
    }
}
